1 module hip.event.handlers.button;
2 
3 public import hip.api.input.button;
4 import hip.util.time;
5 
6 
7 final class HipButtonMetadata : AHipButtonMetadata
8 {
9     import hip.config.opts;
10 
11     float lastDownTime, downTimeStamp;
12     float lastUpTime, upTimeStamp;
13     float timeStartedCheckingRestart = 0;
14     float timeUntilRestartMulticlick = HIP_DEFAULT_TIME_UNTIL_CLICK_COUNT_RESTART;
15     bool _isPressed = false;
16     bool _isNewState = false;
17     ubyte clickCount = 0;
18 
19     override bool isNewState() const {return _isNewState;}
20     override bool isPressed() const {return _isPressed;}
21     override float getLastDownTimeDuration() const {return lastDownTime;}
22     override float getLastUpTimeDuration() const {return lastUpTime;}
23 
24     this(int key){super(key);}
25 
26     override float getDownTimeDuration() const
27     {
28         if(_isPressed)
29             return (HipTime.getCurrentTimeAsMs() - downTimeStamp) / 1000;
30         return 0;
31     }
32     override float getUpTimeDuration() const
33     {
34         if(!_isPressed)
35             return (HipTime.getCurrentTimeAsMs() - upTimeStamp) / 1000;
36         return 0;
37     }
38     override void setPressed(bool press)
39     {
40         _isNewState = false;
41         float currTime = HipTime.getCurrentTimeAsMs();
42         if(currTime - timeStartedCheckingRestart > timeUntilRestartMulticlick)
43         {
44             clickCount = 0;
45             timeStartedCheckingRestart = currTime;
46         }
47         if(press != _isPressed)
48         {
49             _isNewState = true;
50             if(_isPressed)
51             {
52                 lastDownTime = getDownTimeDuration();
53                 upTimeStamp = currTime;
54                 clickCount++;
55             }
56             else
57             {
58                 lastUpTime = getUpTimeDuration();
59                 downTimeStamp = currTime;
60             }
61             _isPressed = press;
62         }
63     }
64 }